﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KeDa2025_.Utils;

namespace KeDa2025_
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //有cookie获取账号密码登录
                HttpCookie account1 = Request.Cookies["account1"];
                HttpCookie pwd1 = Request.Cookies["pwd1"];
                if (account1 != null && pwd1 != null)
                {
                    string sql = string.Format("select * from users where account='{0}'  and  pwd='{1}'", account1.Value, pwd1.Value);
                    MySqlConnection connection = new MySqlConnection("server=localhost;user id=zmy2;password=zmy20020525;database=forum;");
                    MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(sql, connection);
                    DataSet ds = new DataSet();
                    // 填充数据集
                    sqlDataAdapter.Fill(ds);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        Session["User"] = ds.Tables[0].Rows[0];
                        Response.Redirect("index.aspx");
                    }
                }
            }
        
    }

        protected void btLogin_Click(object sender, EventArgs e)
        {//获取账号密码
            string Account = txtName.Text.Trim();
            string Pwd = txtPassword.Text.Trim();
           
            //
            string sql = string.Format("select * from users where  account='{0}'and pwd='{1}'", Account, Pwd);
            MySqlConnection connection = new MySqlConnection("server=localhost;user id=zmy2;password=zmy20020525;database=forum;");
            MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(sql, connection);
            DataSet ds = new DataSet();

            // 打开连接
            connection.Open();
            // 填充数据集
            sqlDataAdapter.Fill(ds);

            if (ds.Tables[0].Rows.Count > 0)
            {
                //正确
                //存会话:把用户的主键存到session
                Session["User"] = ds.Tables[0].Rows[0]["id"];
                if (CheckBox1.Checked)
                {
                    //账号密码存到cookie里
                    HttpCookie accountCookie = new HttpCookie("account1");
                    accountCookie.Value = ds.Tables[0].Rows[0]["account"].ToString();
                    accountCookie.Expires = DateTime.Now.AddMonths(1);
                    Response.Cookies.Add(accountCookie);
                    HttpCookie pwdCookie = new HttpCookie("pwd1");
                    pwdCookie.Value = ds.Tables[0].Rows[0]["pwd"].ToString();
                    pwdCookie.Expires = DateTime.Now.AddMonths(1);
                    Response.Cookies.Add(pwdCookie);

                }
                Response.Redirect("index.aspx");
            }
            else
            {
                Response.Write("<script>alert('账户或密码错误!');</script>");
            }
            // 关闭连接
            connection.Close();
        }

        protected void btBack_Click(object sender, EventArgs e)
        {
            {
                // 清空文本框
                txtName.Text = string.Empty;
                txtPassword.Text = string.Empty;

                // 清除当前会话信息
                Session["User"] = null;

                // 刷新页面
                Response.Redirect(Request.RawUrl);
                Session.Clear();

            }

        }
    }
}